package cn.bupt.adt.util;

import java.util.HashMap;
import java.util.Map;

import cn.bupt.adt.entity.autogenerate.CheckInstiView;
import cn.bupt.adt.entity.autogenerate.DoctorAppUser;
import cn.bupt.adt.entity.autogenerate.InstitutionView;

public class UserUtil {
	/**
	 * 生成token值
	 * @param phone 电话号码
	 * @param lastLoginTime 最后登录时间
	 * @return
	 */
	public static String generateToken(String phone, String lastLoginTime) {
		String token =MD5Util.MD5(phone + lastLoginTime);
		return token;
	}
	/**
	 * 通过token值获取用户id
	 * @param phone
	 * @param lastLoginTime
	 * @return token存在，则返回用户id;否则，返回-1;
	 */
	public static int getUserIdByToken(String key) {
		boolean res = RedisUtil.exist(key);
		if(res){
			int userId = Integer.parseInt((String)RedisUtil.get(key));
			return userId;
		}else{
			return -1;
		}
	}
	public static Map<String,Object> constructUserResp(DoctorAppUser User,String token){
        Map<String,Object> content = new HashMap<String,Object>();
        content.put("id", User.getId());
        content.put("token", token);
        content.put("name", User.getName());
        content.put("phone", User.getPhone());
        content.put("aliAccount", User.getAliAccount());
        content.put("portrait",User.getPortrait());
        content.put("money", User.getMoney());//可提现金额，就是做手术赚的钱
        content.put("waitedToBeWithdrawnMoney", User.getWaitedToBeWithdrawnMoney());
        content.put("riskMargin", User.getRiskMargin());//风险保证金，就是医生充值的钱
        return content;
	}
	public static Map<String,Object> constructInsUserResp(InstitutionView User,String token){
        Map<String,Object> content = new HashMap<String,Object>();
        content.put("id", User.getId());
        content.put("token", token);
        content.put("name", User.getName());
        content.put("phone", User.getPhone());
        content.put("portrait",User.getPortrait());
        content.put("money", User.getMoney());
        content.put("address", User.getAddress());
        content.put("roomNum", User.getRoomNum());
        content.put("waitedToBeWithdrawnMoney", User.getWaitedToBeWithdrawnMoney());
        return content;
	}
	public static Map<String,Object> constructCheckInsUserResp(CheckInstiView ins,String token){
        Map<String,Object> content = new HashMap<String,Object>();
        content.put("id", ins.getId());
        content.put("token", token);
        content.put("name", ins.getName());
        content.put("phone", ins.getPhone());
        return content;
	}
}
